# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: helpers\sales\events.py
__author__ = "Attila Gerendi (Sanyi)"
import logging
from db.mysql import Connection
from helpers.sales.db.tables import OvsEventsTable
from db.tables.users import UserConfirmation

_logger = logging.getLogger("helpers.sales.events")


def create_event(
    event_type,
    user,
    source,
    request_address,
    activation_address=None,
    scans=None,
    registration_address=None,
    verification_counter=0,
):
    try:
        if (
            user["confirmation"]
            in (UserConfirmation.ADMIN_VALIDATED, UserConfirmation.AUTO_VALIDATED)
            or event_type == "user_verification_required"
        ):
            validated_phone = user["extra"].get("validated_phone", None)
        else:
            validated_phone = None
        event_data = dict(
            event_type=event_type,
            user_id=user["user_id"],
            validated_phone=validated_phone,
            email=user["email"],
            name=user["first_name"] if user["first_name"] else "",
            surname=user["last_name"] if user["last_name"] else "",
            country=user["country"] if user["country"] else "",
            company=user["company"] if user["company"] else "",
            phone=user["phone"] if user["phone"] else "",
            website=user["website"] if user["website"] else "",
            license_key=user["license_key"] if user["license_key"] else "",
            registration_address=registration_address,
            activation_address=activation_address,
            request_address=request_address,
            source=source,
            verification_counter=verification_counter,
        )
        if scans:
            event_data["scans_network"] = scans.get("scans_network", 0)
            event_data["scans_network_demo"] = scans.get("scans_network_demo", 0)
            event_data["scans_web"] = scans.get("scans_web", 0)
            event_data["scans_web_demo"] = scans.get("scans_web_demo", 0)
            event_data["scans_total"] = (
                event_data["scans_network"]
                + event_data["scans_network_demo"]
                + event_data["scans_web"]
                + event_data["scans_web_demo"]
            )
        _logger.debug("jb event: %s", event_data)
        with Connection("sales") as (db):
            q = OvsEventsTable.insert().values(**event_data)
            db.execute(q)
    except Exception as e:
        _logger.exception("create_event failed with %s", e)


def new_user(user, request_address):
    create_event(
        "user_registration", user=user, source="ovs", request_address=request_address
    )


def user_extra_verification_required(user, verification_counter, request_address=None):
    create_event(
        "user_verification_required",
        user=user,
        source="ovs",
        request_address=request_address,
        verification_counter=verification_counter,
    )


def user_data_changed(user, request_address, scans=None):
    create_event(
        "user_data_changed",
        user=user,
        source="ovs",
        scans=scans,
        request_address=request_address,
    )


def user_email_changed(user, request_address, scans=None):
    create_event(
        "user_email_changed",
        user=user,
        source="ovs",
        scans=scans,
        request_address=request_address,
    )


def trial_scan_done(user, scans=None):
    create_event(
        "trial_scan_done", user=user, source="ovs", scans=scans, request_address=""
    )


def license_updated(user, request_address, scans=None):
    create_event(
        "license_updated",
        user=user,
        source="ovs",
        scans=scans,
        request_address=request_address,
    )
